﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx.CommonBot.Profiles;
using Styx.CommonBot.AreaManagement;
using Styx;
using Styx.TreeSharp;
using Action = Styx.TreeSharp.Action;
using Styx.CommonBot;
using CommonBehaviors.Actions;
using Styx.Pathing;
using Styx.Common;
using System.Windows.Media;

namespace PetFighterBot
{
    class ProfileHelper
    {
        private static WoWPoint stuckPoint = StyxWoW.Me.Location;
        private static DateTime stuckTime = DateTime.Now;
        private static bool stuck = false;
        private static bool circleBack = false;

        public static void Initialize()
        {
            bool stop = false;
            PetFighter.dlog("Loading Profile ..");
            try
            {
                GrindArea = ProfileManager.CurrentProfile.GrindArea;
                HotspotList = GrindArea.Hotspots.ConvertAll<WoWPoint>(hs => hs.ToWoWPoint());
            }
            catch (Exception ex)
            {
                Logging.WriteException(Colors.Firebrick, ex);
                stop = true;
            }
            if(stop)
                TreeRoot.Stop("can not load Profile!");
        }
        private static Profile _CurrentProfile { get; set; } // { return ProfileManager.CurrentProfile; } }
        private static int _CurrentHotSpot;
        static public List<WoWPoint> HotspotList;
        static public GrindArea GrindArea { get; set; }
        private static WoWPoint getHotSpot { get { return HotspotList.ElementAt(_CurrentHotSpot); } }

        public static Composite CreatePathBehavior()
        {
            return new Sequence(
                    new Action(ret => PetFighter.dlog("[PathBehavior] create patch")),
                    new PrioritySelector(

                        PetFighter.CreateMountUpBehavior,

                        new Decorator(ret => HotspotList.Count == 0,
                            new Sequence(
                                new Action(ret => PetFighter.slog("[ERROR] Profile has no hotspots!")),
                                new Action(ret => TreeRoot.Stop("[ERROR] Profile has no hotspots!")))),
                                
                        new Decorator(ret => _CurrentHotSpot < 0 || _CurrentHotSpot > HotspotList.Count,
                            new Sequence(
                                new Action(ret => PetFighter.dlog("[PatchBehavior] HotSpot = null")),
                                new Action(ret => _CurrentHotSpot = HotspotList.IndexOf(HotspotList.OrderBy(hs => StyxWoW.Me.Location.Distance(hs)).FirstOrDefault()))
                                )),

                        new Decorator(ret => PetFighterSettings.Instance.ProfileRnd,
                            new PrioritySelector(

                                new Decorator(ret => StyxWoW.Me.Location.Distance(getHotSpot) < 50,
                                    new Sequence(
                                        //new Action(ret => _PetFighter.dlog(LogLevel.Diagnostic, "[PathBehavior] looking for new HotSpot ..")),
                                        new Action(ret => _CurrentHotSpot = new Random().Next(HotspotList.Count))
                                        ))
                            )),

                        new Decorator(ret => PetFighterSettings.Instance.ProfileBou,
                            new PrioritySelector(

                                new Decorator(ret => _CurrentHotSpot >= HotspotList.Count,
                                    new Sequence(
                                        new Action(ret => _CurrentHotSpot = 0)
                                    )),

                                new Decorator(ret => StyxWoW.Me.Location.Distance(getHotSpot) < 50,
                                    new Sequence(
                                        new Action(ret => _CurrentHotSpot++)
                                        //new Decorator(ret => _CurrentHotSpot <= HotspotList.Count,
                                            //new Sequence(
                                            //    new ActionSetActivity("[PetFighter] [Navigation] Fly to next Hotspot .."),
                                            //    new Action(ret => _PetFighter.dlog(LogLevel.Diagnostic, "[Navigation] Moving to Hotspot: {0} (Distance: {1})", getHotSpot.ToString(), getHotSpot.Distance(StyxWoW.Me.Location))),
                                            //    new Action(ret => stuckHelper()),
                                            //    new Action(ret => Flightor.MoveTo(getHotSpot))
                                        //))
                                    ))
                                )),

                        new Decorator(ret => PetFighterSettings.Instance.ProfileCir,
                            new PrioritySelector(

                                new Decorator(ret => _CurrentHotSpot == HotspotList.Count && StyxWoW.Me.Location.Distance(getHotSpot) < 50,
                                    new Sequence(
                                        new Action(ret => circleBack = true),
                                        new Action(ret => _CurrentHotSpot--)
                                        )),

                                new Decorator(ret => circleBack && _CurrentHotSpot > 0 && StyxWoW.Me.Location.Distance(getHotSpot) < 50,
                                    new Sequence(
                                        new Action(ret => _CurrentHotSpot--)
                                    )),

                                new Decorator(ret => _CurrentHotSpot == 0 && StyxWoW.Me.Location.Distance(getHotSpot) < 50,
                                    new Sequence(
                                        new Action(ret => circleBack = false),
                                        new Action(ret => _CurrentHotSpot++)
                                        )),

                                new Decorator(ret => !circleBack && _CurrentHotSpot < HotspotList.Count && StyxWoW.Me.Location.Distance(getHotSpot) < 50,
                                    new Sequence(
                                        new Action(ret => _CurrentHotSpot++)
                                    ))

                                )),

                        new Decorator(ret => StyxWoW.Me.Location.Distance(getHotSpot) >= 50,
                            new Sequence(
                                new ActionSetActivity("[PetFighter] [Navigation] Fly to next Hotspot .."),
                                new Action(ret => PetFighter.dlog("[Navigation] Moving to Hotspot: {0} (Distance: {1})", getHotSpot.ToString(), getHotSpot.Distance(StyxWoW.Me.Location))),
                                new Action(ret => stuckHelper()),
                                new Action(ret => Flightor.MoveTo(getHotSpot))
                                )) 
                            

                            ));
            
        }

        
        private static void stuckHelper()
        {
            if (stuckPoint == StyxWoW.Me.Location)
            {
                if (!stuck)
                {
                    stuck = true;
                    stuckTime = DateTime.Now;
                    PetFighter.dlog("set stuckTime [{0}]", DateTime.Now.ToShortTimeString());
                }
            }
            else
            { stuckPoint = StyxWoW.Me.Location; stuck = false; }

            if(stuck && (DateTime.Now - stuckTime) >= TimeSpan.FromSeconds(5))
            {
                PetFighter.dlog("stucked ...");
                _CurrentHotSpot = HotspotList.IndexOf(HotspotList.OrderBy(hs => StyxWoW.Me.Location.Distance(hs)).FirstOrDefault());
                stuck = false;
            }
        }


    }
}
